package chs;

import static org.junit.Assert.assertTrue;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.testing.RobotTestBed;
import robocode.control.snapshot.IBulletSnapshot;

/**
 * Tests that Atom fires shots at a specific power corresponding to distance.
 *
 * @author Sean Chung
 */
public class TestAtomFiring extends RobotTestBed {
  // Check that power is constant
  boolean expectedPower;
  // Check that power eventually reaches maximum
  boolean maxPower;

  /**
   * Specifies that SittingDuck and Atom are to be matched up in this test case.
   *
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,chs.Atom";
  }

  /**
   * This test runs for 10 round.
   *
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * Check that all bullets fired are at power 3.0 or expected power, 
   * since SittingDuck does not move, distance can be set as constant.
   *
   * @param event Info about the current state of the battle.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    IBulletSnapshot bullets[] = event.getTurnSnapshot().getBullets();    
    double bulletPower;
    
    // Expected power for all bullets, SittingDuck is placed in center, does not move.
    // The distance will always be 400 or 300.
    final double firstExpectedPower = 3.0 - ((400.0 / 100.0) * 0.5);
    final double secondExpectedPower = 3.0 - ((300.0 / 100.0) * 0.5);
   
    for (IBulletSnapshot bullet : bullets) {
      bulletPower = bullet.getPower();
      // Power of initial bullets are expected to be a certain constant power  
      if (comparePower(bulletPower, firstExpectedPower)) {
        expectedPower = true;
      }
      else if (comparePower(bulletPower, secondExpectedPower)) {
        expectedPower = true; 
      }
      // If bullets consistently hit power is automatically increased to 3.0
      // Since SittingDuck does not move, this is expected
      else if (bulletPower == 3.0) {
        maxPower = true;
      }
      // If even one of the bullets are not one of the three possible powers, cannot be true.
      else {
        expectedPower = false;
        break;
      }
    }    
  }
  
  /**
   * Calculates whether a bullets current power is within a reasonable difference
   * from each other.
   * 
   * @param power the power of bullet
   * @param expectedPower the expected power of bullet
   * @return true if within 0.000001; false otherwise
   */
  public boolean comparePower(double power, double expectedPower) {
    boolean samePower = false;
    if ((Math.abs(expectedPower - power))  < 0.000001) {
      samePower = true;
    }
    return samePower;
  }
  
  
  /**
   * Returns a comma or space separated list like: x1,y1,heading1, x2,y2,heading2, which are the 
   * coordinates and heading of robot #1 and #2. So "0,0,180, 50,80,270" means that robot #1 
   * has position (0,0) and heading 180, and robot #2 has position (50,80) and heading 270.
   * 
   * Override this method to explicitly specify the initial positions for your test cases. 
   * 
   * Defaults to null, which means that the initial positions are determined randomly.  Since
   * battles are deterministic by default, the initial positions are randomly chosen but will
   * always be the same each time you run the test case. 
   * 
   * @return The list of initial positions.
   */
  @Override
  public String getInitialPositions() {
    return "400,400,90, 0,0,0";
  }

  /**
   * After the battle, check to see that bullets of expected power.
   *
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {    
    assertTrue("Expected Power", expectedPower);
    assertTrue("Max Power", maxPower);
  }  
}
